#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@AUTHOR: tyc
@VERSION: 1.0.0
@DESCRIPTION:  爬取豆瓣前250名的电影名 和 年份
@time: 2021-07-01 22:11:56 
"""

import requests
import re

url = "https://movie.douban.com/top250"
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
}

resp = requests.get(url, headers=headers)
s = resp.text


obj = re.compile(r'<li>.*?div class="item">.*?<span class="title">(?P<name>.*?)</span>.*?<div class="bd">.*?<br>(?P<year>.*?)&nbsp.*?<span class="rating_num" property="v:average">(?P<score>.*?)</span>', re.S)


for item in re.finditer(obj, s):
    y = item.group("year").strip()
    n = item.group("name").strip()
    f = item.group('score').strip()
    # print(item.group("name"))
    print(f'{y}年电影：<<{n}>> 评分为：{f}')

